using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Deepcode.Flux.Core.Collections;

public partial class Setup_Install_Panels_SetupDatabase: Deepcode.Flux.Core.UI.Abstract.SetupWizardBase
{
	protected override Panel Base_ErrorContainer{ get{ return this.localErrorHost; }}
	protected override Label Base_ErrorLabel { get { return this.localErrors; } }

	protected void Page_Load(object sender, EventArgs e)
	{
	}

	/// <summary>
	/// Form is being populated
	/// </summary>
	/// <param name="settings"></param>
	public override void Initialise(Deepcode.Flux.Core.Collections.BaseDictionary settings)
	{
		base.Initialise(settings);

		SQLServer.Text = settings["SQLServer"];
		SQLServerDatabase.Text = settings["SQLServerDatabase"];
		SQLDatabaseExists.Checked = (settings["SQLDBExists"] == "YES");
		SQLUsername.Text = settings["SQLServerUsername"];
		SQLPassword.Text = settings["SQLServerPassword"];
		if( settings["SQLAuthMode"] == "WINDOWS" )
		{
			SQLAuth_SQL.Checked = false;
			SQLAuth_Win.Checked = true;
		}
		else
		{
			SQLAuth_SQL.Checked = true;
			SQLAuth_Win.Checked = false;
		}
		
		Auth_CheckChange( null, EventArgs.Empty );
	}

	/// <summary>
	/// Form is being saved
	/// </summary>
	/// <param name="settings"></param>
	/// <returns></returns>
	public override bool FinishForm(Deepcode.Flux.Core.Collections.BaseDictionary settings)
	{
		// Ensure basic fields are filled in
		if (SQLServer.Text == "") return DoLocalError("You must specify the IP or name of the SQL Server to connect to");
		if (SQLServerDatabase.Text == "") return DoLocalError("You must specify the name of the SQL Database to use");

		// Copy form to the dictionary
		settings["SQLServer"] = SQLServer.Text;
		settings["SQLServerDatabase"] = SQLServerDatabase.Text;
		settings["SQLDBExists"] = (SQLDatabaseExists.Checked ? "YES" : "NO");
		settings["SQLServerUsername"] = SQLUsername.Text;
		settings["SQLServerPassword"] = SQLPassword.Text;
		settings["SQLAuthMode"] = (SQLAuth_SQL.Checked ? "SQL" : "WINDOWS");

		// Test connection to the SQL Server to ensure server name is correct
		string ServerString = SetupDictionarySession.GetConnectionString(false);
		string DBString = SetupDictionarySession.GetConnectionString(true);
		if (!CanConnect(ServerString))
		{
			this.DoLocalError("Cannot connect to the SQL Server with those settings");
			return false;
		}
		// Test connection to the database if it's supposed to exist
		if (SetupDictionarySession.Config["SQLDBExists"] == "YES")
		{
			if (!CanConnect(DBString))
			{
				this.DoLocalError("Connected to the SQL Server, but couldn't connect to that database");
				return false;
			}
		}
		// Ensure database doesn't exist if it's not supposed to
		else
		{
			if (CanConnect(DBString))
			{
				this.DoLocalError("The database specified already exists - choose a database that does not exist or turn on the already created checkbox");
				return false;
			}
		}
		return true;
	}

	/// <summary>
	/// Authentication mechanism is being changed
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Auth_CheckChange(object sender, EventArgs e)
	{
		if (SQLAuth_SQL.Checked)
		{
			this.SQLUsername.Enabled = true;
			this.SQLPassword.Enabled = true;
			this.SQLAuthPanel.Enabled = true;
		}
		else
		{
			this.SQLUsername.Enabled = false;
			this.SQLPassword.Enabled = false;
			this.SQLAuthPanel.Enabled = false;
		}
	}
	
	/// <summary>
	/// Check if a connection can be made using the SQL connection string passed in.
	/// </summary>
	/// <param name="connection"></param>
	/// <returns></returns>
	private bool CanConnect(string connection)
	{
		SqlConnection.ClearAllPools();
		SqlConnection c = null;
		try
		{
			c = new SqlConnection(connection);
			c.Open();
			c.Close();
			return true;
		}
		catch
		{
			if (c != null) c.Close();
			return false;
		}
	}
}
